package com.example.myapplication.adp;

import android.app.Activity;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.myapplication.R;
import com.example.myapplication.javaBean.PhotoBean;
import com.example.myapplication.util.GlideUtil;
import com.example.myapplication.util.easyphoto.GlideEngine;
import com.huantansheng.easyphotos.EasyPhotos;
import com.huantansheng.easyphotos.callback.SelectCallback;
import com.huantansheng.easyphotos.models.album.entity.Photo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PhotoUpAdp extends BaseQuickAdapter<PhotoBean, BaseViewHolder> {

    public PhotoUpAdp(@Nullable List<PhotoBean> data) {
        super(R.layout.photo_adp_item, data);
        if (data.size()==0){
            data.add(new PhotoBean(null,2));
        }
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, PhotoBean item) {
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) baseViewHolder.itemView.getLayoutParams();
        switch (baseViewHolder.getAdapterPosition()%4){
            case 0:
                params.setMargins(0,0,4,10);
                break;
            case 1:
            case 2:
                params.setMargins(4,0,4,10);
                break;
            case 3:
                params.setMargins(4,0,0,10);
                break;
        }

        ImageView itemImg = baseViewHolder.getView(R.id.itemImg);
        View btAdd = baseViewHolder.getView(R.id.itemAdd);
        View btDelete = baseViewHolder.getView(R.id.btDelete);

        if (item.getType()==1){
            GlideUtil.loadImg(item.getPath(), itemImg);
            btDelete.setVisibility(View.VISIBLE);
            btAdd.setVisibility(View.GONE);
        }else {
            itemImg.setImageBitmap(null);
            btAdd.setVisibility(View.VISIBLE);
            btDelete.setVisibility(View.GONE);
        }
        btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                remove(item);
            }
        });
        btAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyPhotos.createAlbum(((Activity) getContext()),false,true,new GlideEngine())
                        .setCount(8-getItemCount())
                        .start(new SelectCallback() {
                            @Override
                            public void onResult(ArrayList<Photo> photos, boolean isOriginal) {
                                //先移+
                                Iterator<PhotoBean> iterator = getData().iterator();
                                while (iterator.hasNext()){
                                    PhotoBean next = iterator.next();
                                    if (next.getType()==2){
                                        remove(next);
                                    }
                                }
                                //加图
                                for (int i = 0; i < photos.size(); i++) {
                                    addData(new PhotoBean(photos.get(i).path,1));
                                }
                                //加+
                                if (getItemCount()<8)
                                addData(new PhotoBean(null,2));

                            }

                            @Override
                            public void onCancel() {

                            }
                        });
            }
        });


    }
}
